<template>
	<view class="department">
		<view class="search">
			<!-- search 事件,用户点击回车或者点击手机的搜索按钮  @custom:点击右侧控件-->
			<u-search placeholder="请输入科室" :showAction="true" @custom="onsearch" v-model="value"
				@search="onsearch"></u-search>
		</view>
		<view class="department-wrap">
			<!-- v-model:main-active-index:左侧选中的索引;	v-model:active-id:右侧选中的索引 -->
			<van-tree-select v-model:active-id="activeId" v-model:main-active-index="activeIndex" :items="olditems" 	@click-item="chooseitem" />

		</view>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onHide,
		onInit,
		onLaunch,
		onReady,
		onResize,
		onShow,
		onUnload
	} from "@dcloudio/uni-app"
	import { ref, onBeforeMount } from 'vue'
	import { yuYue } from '../../pinia/yuyue'
	const yuyue = yuYue()
	
	let value = ref('')
	// 发送请求,获取所有的科室列表
	let activeId = ref(1);
	let activeIndex = ref(0);
	let olditems = ref([
		{
			text: '新生儿科',
			children: [{
				text: '新生门诊',
				id: 1
			}],
		},
		{
			text: '血液内科',
			children: [{
				text: '血液内科门诊',
				id: 2
			}],
		},
		{
			text: '心血管科',
			children: [{
					text: '冠心病专科门诊',
					id: 3
				},
				{
					text: '高血压专病门诊',
					id: 4
				},
				{
					text: '心脏康复专病门诊',
					id: 5
				},
			]
		},
		{
			text: '消化内科',
			children: [{
					text: '消化肝病门诊',
					id: 6
				},
				{
					text: '消化介入门诊',
					id: 7
				},
				{
					text: '消化内科门诊',
					id: 8
				},
			]
		},
	])
	let baseArr = JSON.parse(JSON.stringify(olditems.value))
	
	// 获得搜索的value,发送请求
	function onsearch(val) {
		if(!val) return olditems.value=baseArr
		let n =[]
		baseArr.forEach(item=>{
			if(item.text.includes(val))  n.push(item)
		})
		olditems.value = n
	}
	
	// 选择了最右侧的科室,携带科室信息,跳到对应科室详情页,并将对应的科室信息存到pinia中
	const chooseitem = ((vv) => {
		yuyue.updateDepartment(vv)
		uni.$u.route({
			url: '/views/finadepartment/finadepartment',
			params: vv
		})

	})
	onBeforeMount(() => {
		 value.value = ''
	
	})
	
	// 获取上个页面传递过来的科室信息,
	onLoad((params) => {
		// 根据科室信息发送请求,获得对应的科室上班医生,或者通过日期预约,得到该科室下的近期在职医生
		let n =[]
		value.value = params.val
		// baseArr.forEach(item=>{
		// 	if(item.text.includes(params.val))  n.push(item)
		// })
		// console.log(baseArr)
		// olditems.value = n
	
	})
</script>

<style lang="scss">
	.department {
		width: 100%;
		height: calc(100vh - 44px);
		overflow: scroll;

	}

	.search {
		margin: 10px 0;
	}

	.department-wrap {
		height: calc(100vh - 94px);
		background-color: #f7f8fa;
	}
</style>